function longestWord(words: string[]): string {
  words.sort((a, b) =>
    a.length === b.length ? a.localeCompare(b) : a.length - b.length
  );

  // console.log(words);
  let length = words.length - 1;
  while (length >= 0) {
    if (words[length].length !== words[length - 1].length) {
      return words[length];
    }
  }
  // return words[0]
}
const words = ["a", "banana", "app", "appl", "ap", "apply", "apple"];
console.log(longestWord(words));
